<template>
  <section class="config-section">
    <div class="section-header">
      <span class="section-icon">{{ icon }}</span>
      <h3 class="section-title">{{ title }}</h3>
    </div>
    
    <div class="config-list">
      <div 
        v-for="(value, key) in config" 
        :key="key" 
        class="config-item"
        :class="{ 'config-item-empty': isValueEmpty(value) }"
      >
        <div class="config-key">{{ key }}</div>
        <div class="config-value" :class="getValueClass(value)">
          <span v-if="isValueEmpty(value)" class="empty-value">未配置</span>
          <span v-else-if="typeof value === 'boolean'" class="boolean-value">
            {{ value ? '✅ 启用' : '❌ 禁用' }}
          </span>
          <span v-else-if="isUrl(value)" class="url-value">
            <a :href="value" target="_blank" rel="noopener noreferrer">{{ value }}</a>
          </span>
          <span v-else-if="isLongText(value)" class="long-text-value">
            <span class="text-preview">{{ getTextPreview(value) }}</span>
            <button @click="toggleExpand(key)" class="expand-btn">
              {{ expandedItems.includes(key) ? '收起' : '展开' }}
            </button>
            <div v-if="expandedItems.includes(key)" class="text-full">
              {{ value }}
            </div>
          </span>
          <span v-else class="normal-value">{{ value }}</span>
        </div>
      </div>
    </div>
  </section>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'

interface Props {
  title: string
  config: Record<string, any>
  icon: string
}

const props = defineProps<Props>()

// 响应式数据
const expandedItems = ref<string[]>([])

// 计算属性
const isValueEmpty = (value: any): boolean => {
  return value === '' || value === null || value === undefined || value === '未配置'
}

const isUrl = (value: string): boolean => {
  if (typeof value !== 'string') return false
  return value.startsWith('http://') || value.startsWith('https://')
}

const isLongText = (value: string): boolean => {
  if (typeof value !== 'string') return false
  return value.length > 50
}

const getTextPreview = (value: string): string => {
  return value.length > 50 ? value.substring(0, 50) + '...' : value
}

const getValueClass = (value: any): string => {
  if (isValueEmpty(value)) return 'value-empty'
  if (typeof value === 'boolean') return 'value-boolean'
  if (isUrl(value)) return 'value-url'
  if (isLongText(value)) return 'value-long-text'
  return 'value-normal'
}

// 方法
const toggleExpand = (key: string) => {
  const index = expandedItems.value.indexOf(key)
  if (index > -1) {
    expandedItems.value.splice(index, 1)
  } else {
    expandedItems.value.push(key)
  }
}
</script>

<style lang="scss" scoped>
.config-section {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  padding: 1.5rem;
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
  
  &:hover {
    background: rgba(255, 255, 255, 0.15);
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
  }
}

.section-header {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  
  .section-icon {
    font-size: 1.5rem;
    width: 2rem;
    height: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
  }
  
  .section-title {
    margin: 0;
    font-size: 1.3rem;
    font-weight: 600;
    color: #ffd700;
  }
}

.config-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.config-item {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 1rem;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  transition: all 0.3s ease;
  
  &:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.3);
  }
  
  &.config-item-empty {
    opacity: 0.7;
    
    .config-key {
      color: #95a5a6;
    }
  }
}

.config-key {
  font-weight: 600;
  color: #ffd700;
  min-width: 120px;
  margin-right: 1rem;
  font-size: 0.95rem;
}

.config-value {
  flex: 1;
  text-align: right;
  word-break: break-all;
  
  &.value-empty {
    color: #95a5a6;
    font-style: italic;
  }
  
  &.value-boolean {
    font-weight: 600;
    
    .boolean-value {
      padding: 0.3rem 0.8rem;
      border-radius: 20px;
      font-size: 0.9rem;
      
      &:contains('✅') {
        background: rgba(39, 174, 96, 0.2);
        color: #2ecc71;
      }
      
      &:contains('❌') {
        background: rgba(231, 76, 60, 0.2);
        color: #e74c3c;
      }
    }
  }
  
  &.value-url {
    .url-value a {
      color: #3498db;
      text-decoration: none;
      word-break: break-all;
      
      &:hover {
        text-decoration: underline;
        color: #5dade2;
      }
    }
  }
  
  &.value-long-text {
    .text-preview {
      display: block;
      margin-bottom: 0.5rem;
      color: #ecf0f1;
    }
    
    .expand-btn {
      background: rgba(52, 152, 219, 0.2);
      color: #3498db;
      border: 1px solid rgba(52, 152, 219, 0.3);
      padding: 0.3rem 0.8rem;
      border-radius: 4px;
      font-size: 0.8rem;
      cursor: pointer;
      transition: all 0.3s ease;
      
      &:hover {
        background: rgba(52, 152, 219, 0.3);
        border-color: rgba(52, 152, 219, 0.5);
      }
    }
    
    .text-full {
      margin-top: 0.5rem;
      padding: 0.8rem;
      background: rgba(0, 0, 0, 0.2);
      border-radius: 4px;
      font-family: 'Courier New', monospace;
      font-size: 0.9rem;
      white-space: pre-wrap;
      word-break: break-all;
    }
  }
  
  &.value-normal {
    color: #ecf0f1;
  }
}

.empty-value {
  color: #95a5a6;
  font-style: italic;
}

// 响应式设计
@media (max-width: 768px) {
  .config-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }
  
  .config-key {
    min-width: auto;
    margin-right: 0;
  }
  
  .config-value {
    text-align: left;
    width: 100%;
  }
  
  .section-header {
    flex-direction: column;
    text-align: center;
    gap: 0.5rem;
  }
}
</style> 